<?php
/**
 * @ClassName: Dictdata
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-06 17:07
 * @Version: V1.0.0
 */

namespace app\admin\controller;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Dictdata extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected $dict_type = 0;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\DictData();
        $this->pageTitle = '字典数据';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->selectSearch = 'dict_label';
        $this->selectTextField = '{id}#{dict_label}';

        $this->indexWith = ['dict'];

        $this->dict_type = input('dict_type', 0);
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        if ($this->dict_type == 0) {
            $search->select('dict_type', '字典类型')->dataUrl(url('/admin/dicttype/selectPage'));
        }
        $search->text('dict_label', '字典标签');
        $search->select('status', '状态')->options([1 => '启用', 0 => '停用']);
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if ($this->dict_type) {
            $where[] = ['dict_type', 'eq', $this->dict_type];
        }
        if (isset($searchData['dict_type']) && $searchData['dict_type'] != '') {
            $where[] = ['dict_type', 'eq', $searchData['dict_type']];
        }
        if (!empty($searchData['dict_label'])) {
            $where[] = ['dict_label', 'like', '%' . $searchData['dict_label'] . '%'];
        }
        if (isset($searchData['status']) && $searchData['status'] != '') {
            $where[] = ['status', 'eq', $searchData['status']];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');

        $table->show('dict_label', '字典标签');
        $table->show('dict_value', '字典键值');
        $table->match('dict_type', '字典类型')->to('{dict_type}#{dict.name}');
        $table->switchBtn('is_show', '是否显示')->autoPost('', true);
        $table->switchBtn('status', '状态')->autoPost('', true);
        $table->match('is_default', '是否默认')->options(['Y' => '是', 'N' => '否']);
        $table->text('sort', '排序')->autoPost('', true);

        $table->show('create_time', '添加时间');
        $table->show('update_time', '修改时间');

        $table->sortable('id, sort, create_time, update_time');

        $table->getToolbar()
            ->btnAdd(url('add', ['dict_type' => $this->dict_type]))
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $data['dict_type'] = input('dict_type', 0);

        if ($data['dict_type'] == 0) {
            $form->select('dict_type', '字典类型')->dataUrl(url('/admin/dicttype/selectPage'))->required();
        } else {
            $form->hidden('dict_type');
        }

        $form->text('dict_label', '字典标签')->required();
        $form->text('dict_value', '字典键值')->required();
        $form->radio('is_show', '是否显示')->options([1 => '显示', 0 => '不显示'])->default(1)->required();
        $form->radio('status', '状态')->options([1 => '启用', 0 => '停用'])->default(1)->required();
        $form->radio('is_default', '是否默认')->options(['Y' => '是', 'N' => '否'])->default('N')->required();
        $form->number('sort', '排序')->required();

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '修改时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'dict_type',
            'dict_label',
            'dict_value',
            'dict_value',
            'is_show',
            'status',
            'is_default',
            'sort'
        ], 'post');

        $result = $this->validate($data, []);

        if (true !== $result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();

            $res = $this->dataModel->where('dict_value', '=', $data['dict_value'])->where('dict_type', '=', $data['dict_type'])->where('id', '<>', $id)->find();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();

            $res = $this->dataModel->where('dict_value', $data['dict_value'])->where('dict_type', '=', $data['dict_type'])->find();
        }

        if ($res) {
            $this->error('字典键值已经存在');
        }

        return $this->doSave($data, $id);
    }

}